package com.advertising.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.advertising.model.Release;
import com.tgyt.common.tools.page.Pagination;
import com.tgyt.framework.dao.hibernate.Finder;
import com.tgyt.framework.dao.hspring.BaseDAO;
import com.tgyt.framework.dao.hspring.handler.IHandler;

@Repository(value = "releaseDAO")
public class ReleaseDAO extends BaseDAO<Release> {

	/**
	 * @Title: getPageList
	 * @Description: 具体某页显示多少条记录
	 * @param @param user 一条具体的用户信息
	 * @param @param page 当前页
	 * @param @param rows 一页显示多少条记录
	 * @param @return
	 * @return Pagination
	 * @throws
	 */
	public Pagination getPageList(Release release, int page, int rows,
			String sort, String order) {
		StringBuffer sb = new StringBuffer("from Release where 1=1");

		if (null != release) {
			 if(null!=release.getTitle()&&!"".equals(release.getTitle())){
				 sb.append(" and title like '%"+release.getTitle()+"%'");
			 }
			if(null!=release.getAdverstate()&&!"".equals(release.getAdverstate())){
				 sb.append(" and adverstate like '%"+release.getAdverstate()+"%'");
				 
			}
			if(null!=release.getRadius()&&release.getRadius()!=0.0){
				  Double  x_latitude = release.getLatitude()+release.getRadius();
				  Double  y_latitude = release.getLatitude()-release.getRadius();
				  Double  x_longitude = release.getLongitude()+release.getRadius();
				  Double  y_longitude = release.getLongitude()-release.getRadius();
				 sb.append(" and latitude between "+y_latitude+" and "+x_latitude);
				 sb.append(" and longitude between "+y_longitude+" and "+x_longitude);
			}
			
			if (null!=release.getUser()) {
				 
				sb.append(" and user like '%"+release.getUser().getId()+"%'");
				 
			}
			if (null!=release.getIntroduction()) {
				 
				sb.append(" and introduction like '%"+release.getIntroduction()+"%'");
				 
			}

		}
		//sb.append(" order by  desc");

		if (sort != null && !"".equals(sort)) {
			sb.append(" order by " + sort);
			if (order != null && !"".equals(order)) {
				sb.append(" " + order);
			} else {
				sb.append(" desc");
			}
		}

		return super.getHandler().getPageList(Finder.create(sb.toString()),
				page, rows);
	}

	/**
	 * @Title: getMaxOrder
	 * @Description:获取操作信息表中的排序最大值
	 * @param @return
	 * @return int
	 * @throws
	 */
	public int getMaxOrder() {
		IHandler handler = this.getHandler();
		Object result = handler.findObj("select max(orderid) from Release ");
		if (result != null) {
			return (Integer) result;
		} else {
			return 0;
		}

	}

	/**
	 * @Title: getAll
	 * @Description: 获得所有记录
	 * @param @return
	 * @return List<Actions>
	 * @throws
	 */
	public List<Release> getAll() {
		return this.findList("from Release");

	}
	
 
	
	
	public String findImageUrl(String userid){
		StringBuffer hql = new StringBuffer("from Release where user.id=");
		hql.append(userid);
		Release release = super.find(hql.toString());
		return release.getPhoto();
	}
}
